package com.tbit.uqbike.webmanager.controller;

import cn.hutool.core.util.StrUtil;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.BusDesConfig;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.BusDesConfigService;
import com.tbit.uqbike.webmanager.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 业务自定义描述类型(BusDesConfig)表控制层
 *
 * @author jing
 * @since 2020-12-30 16:50:46
 */
@RestController
@RequestMapping("/busdesconfig")
public class BusDesConfigController {
    /**
     * 服务对象
     */
    @Resource
    private BusDesConfigService busDesConfigService;

    /**
     * 通过主键查询单条数据
     *
     * @param accountId 主键
     * @return 单条数据
     */
    @PostMapping("/selectList")
    public JsonResult selectList(Integer accountId) {
        return JsonResult.ok(this.busDesConfigService.queryById(accountId));
    }

    /**
     * @param busDesConfig
     * @return com.tbit.uqbike.object.view.JsonResult
     * @Description: 修改和插入购买须知
     * @createDate 2020/12/30 17:20
     */
    @PostMapping("/edit")
    @OperateLog(module = ModuleEnum.NOTICETHAT, methods = MethodEnum.NOTICETHAT_UPDATE)
    public JsonResult edit(BusDesConfig busDesConfig) {
        Assert.notNull(busDesConfig.getAccountId(), "ver.accountId.notNull");
        Assert.notNull(busDesConfig.getBusType(), "ver.AccountDepositBound.type.notNULL");
        Assert.notNull(busDesConfig.getDes(), "ver.busDesConfig.desc.notNULL");
        if (busDesConfig.getDes().length() > 500){
            throw new BaseException("购买须知描述过长");
        }else if (StrUtil.isNotEmpty(busDesConfig.getRemark()) && busDesConfig.getRemark().length() > 255){
            throw new BaseException("购买须知备注过长");
        }
        busDesConfig.setUpdateTime(new Date());
        busDesConfigService.insert(busDesConfig);
        return JsonResult.ok();
    }

    @PostMapping("/insert")
    @OperateLog(module = ModuleEnum.NOTICETHAT, methods = MethodEnum.NOTICETHAT_ADD)
    public JsonResult insert(BusDesConfig busDesConfig) {
        Assert.notNull(busDesConfig.getAccountId(), "ver.accountId.notNull");
        Assert.notNull(busDesConfig.getBusType(), "ver.AccountDepositBound.type.notNULL");
        Assert.notNull(busDesConfig.getDes(), "ver.busDesConfig.desc.notNULL");
        BusDesConfig b = busDesConfigService.query(busDesConfig.getAccountId(), busDesConfig.getBusType());
        if (b != null) {
            throw new BaseException(SystemCodeEnum.SUCH_INSTRUCTIONS_HAS_BEEN_ADDED);
        }
        return edit(busDesConfig);
    }

    /**
     * @param busDesConfig
     * @return com.tbit.uqbike.object.view.JsonResult
     * @Description: 删除购买须知
     * @createDate 2020/12/30 17:20
     */
    @PostMapping("/delete")
    @OperateLog(module = ModuleEnum.NOTICETHAT, methods = MethodEnum.NOTICETHAT_DEL)
    public JsonResult delete(BusDesConfig busDesConfig) {
        Assert.notNull(busDesConfig.getAccountId(), "ver.accountId.notNull");
        Assert.notNull(busDesConfig.getBusType(), "ver.AccountDepositBound.type.notNULL");
        busDesConfigService.deleteById(busDesConfig.getAccountId(), busDesConfig.getBusType());
        return JsonResult.ok();
    }

}